<?php
/**
 * 框架主体入口
 * 
 * @package SFramework
 * @author Shuky
 * @since v1.0
 * @version $Id: Main.php 99 2011-04-06 17:47:53Z shukyoo@gmail.com $
 */
namespace SF;

// 定义框架路径
define('SF_PATH', dirname(__FILE__));

defined('APP_PATH') || exit('需要定义APP_PATH');


// 框架缓存
if (false) {
	if (!is_file(APP_PATH .'/tmp/sf_runtime.php')) {
		$content = '';
		$content .= file_get_contents(SF_PATH .'/Request.php');
		$content .= file_get_contents(SF_PATH .'/Router.php');
		$content .= file_get_contents(SF_PATH .'/Registry.php');
		$content .= file_get_contents(SF_PATH .'/Exception.php');
		//$content .= file_get_contents(APP_PATH .'/common.conf.php');
		$content = '<?php'. str_replace('<?php', '', $content);
		file_put_contents(APP_PATH .'/tmp/sf_runtime.php', $content);
		unset($content);
	}
	require_once APP_PATH .'/tmp/sf_runtime.php';
}


/**
 * 自动加载
 * @TODO 关于自动加载的大小写容错优化
 */
spl_autoload_register('\SF\autoload');
function autoload($className)
{
	$className = str_replace('\\', '/', $className);
	$rn = strstr($className, '/', true);
	switch ($rn) {
		case 'APP':
			$nd = array(
				'/C/' => '/controllers/',
				'/M/' => '/models/',
				'/INC/' => '/includes/',
				'/PUT/' => '/inputs/',
			);
			foreach ($nd as $n=>$d) {
				if (strpos($className, $n)) {
					$classFile = APP_PATH . $d . substr(strstr($className, $n), strlen($n)) .'.php';
					break;
				}
			}
			break;
		case 'T':
			$classFile = APP_PATH .'/tables'. strstr($className, '/') .'.php';
			break;
		default:
			$classFile = SF_PATH . strstr($className, '/') .'.php';
			break;
	}
	if (!isset($classFile) || !is_file($classFile)) {
		throw new Exception($className .' 对应的文件不存在：'. $classFile);
	}
	include_once $classFile;
}